package com.example.demo.controller;


import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RequestMapping("/trans")
@RestController
public class TransactionalController {
    @Autowired
    private UserService userService;

    //没有error和运行时异常时,会提交事务,否则会回滚
    @Transactional
    @RequestMapping("/r1")
    public String registry(String name, String password) {
        //⽤⼾注册
        userService.registryUser(name, password);
        //强制程序抛出异常
        int a = 10/0;

        return "注册成功";
    }

    //对异常进行捕获,会提交事务
    @Transactional
    @RequestMapping("/r2")
    public String registry2(String name, String password) {
        //⽤⼾注册
        userService.registryUser(name, password);
        //对异常进⾏捕获
        try {
            //强制程序抛出异常
            int a = 10/0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "注册成功";
    }

    //重新抛出异常,事务会回滚
    @Transactional
    @RequestMapping("/r3")
    public String registry3(String name, String password) {
        //⽤⼾注册
        userService.registryUser(name, password);
        //对异常进⾏捕获
        try {
            //强制程序抛出异常
            int a = 10/0;
        } catch (Exception e) {
            e.printStackTrace();
            //将异常重新抛出去
            throw e;
        }
        return "注册成功";
    }

    //捕获了异常,但手动回滚了
    @Transactional
    @RequestMapping("/r4")
    public String registry4(String name, String password) {
        //⽤⼾注册
        userService.registryUser(name, password);
        //对异常进⾏捕获
        try {
            //强制程序抛出异常
            int a = 10/0;
        } catch (Exception e) {
            e.printStackTrace();
            // ⼿动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    //除了error和runtime异常,其他不会进行回滚
    @Transactional
    @RequestMapping("/r5")
    public String registry5(String name, String password) throws IOException {
        //⽤⼾注册
        userService.registryUser(name, password);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }

    //配置rollbackFor, 所有异常类都会执行回滚
    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r6")
    public String registry6(String name, String password) throws IOException {
        //⽤⼾注册
        userService.registryUser(name, password);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }
    //配置rollbackFor,设置隔离级别
    @Transactional(isolation = Isolation.DEFAULT)
    @RequestMapping("/r7")
    public String registry7(String name, String password) throws IOException {
        //⽤⼾注册
        userService.registryUser(name, password);
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }
}
